package com.hhxy.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 测试重定向
 * @author ghp
 * @date 2022/9/13
 */
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取前端传过来的数据
        //1.1 设置数据的编码格式，防止中文乱码
        request.setCharacterEncoding("utf-8");
        //1.2 获取数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2、判断用户名和密码是否正确（这一步是模拟查数据库）
        if("张三".equals(username) && "123".equals(password)){
            //用户名和密码都正确，重定向到welcome.html页面
            response.sendRedirect("/day03_Fourth/html/welcome.html");
        }else{
            //登录失败，重定向到log.html（感觉这一步好**）
            response.sendRedirect("/day03_Fourth/html/login.html");
        }
    }
}